
#include "test.h"
#include <stdlib.h>
#include <string.h>

#include <stdio.h> 
#include <stdbool.h> 

/**
 * @brief  LED Init
 * @param  None
 * @retval None
 */
void LED_Init(void)
{
    stc_gpio_init_t stcGpioInit;

    (void)GPIO_StructInit(&stcGpioInit);
    stcGpioInit.u16PinState = PIN_STAT_RST;
    stcGpioInit.u16PinDir = PIN_DIR_OUT;
    (void)GPIO_Init(LED_PORT, LED_PIN, &stcGpioInit);
}


/**
 * Definition for singly-linked list. */

//typedef struct ListNode 
//{
//	int val;
//	struct ListNode *next;
//}node_s ;

struct ListNode 
{
	int val;
	struct ListNode *next;
}  ;


struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2)
{
    struct ListNode *head = NULL, *tail = NULL;
    int carry = 0;
    while (l1 || l2) 
    {
        int n1 = l1 ? l1->val : 0;
        int n2 = l2 ? l2->val : 0;
        int sum = n1 + n2 + carry;
        if (!head)
        {
            head = tail = malloc(sizeof(struct ListNode));
            tail->val = sum % 10;
            tail->next = NULL;
        }
        else
        {
            tail->next = malloc(sizeof(struct ListNode));
            tail->next->val = sum % 10;
            tail = tail->next;
            tail->next = NULL;
        }
        carry = sum / 10;
        if (l1)
        {
            l1 = l1->next;
        }
        if (l2)
        {
            l2 = l2->next;
        }
    }
    if (carry > 0) {
        tail->next = malloc(sizeof(struct ListNode));
        tail->next->val = carry;
        tail->next->next = NULL;
    }
    return head;
}

void test1(void)
{
	struct ListNode* a1=NULL;
 	struct ListNode* a2=NULL;
	struct ListNode* b1=NULL;
 	struct ListNode* b2=NULL;
 	struct ListNode* c; 

	a1 = malloc(sizeof(struct ListNode));
	a2 = malloc(sizeof(struct ListNode));
	b1 = malloc(sizeof(struct ListNode));
	b2 = malloc(sizeof(struct ListNode));
	
	c = malloc(2*sizeof(struct ListNode));
	a1->next = a2;
	b1->next = b2;
	a1->val=1;
	a2->val=2;
	
	b1->val=3;
	b2->val=4;
	
	c=addTwoNumbers(a1,b1);
}


#define SIZE 50
char g_arry[50];

char * mergeAlternately(char * word1, char * word2) 
{
    char* res = malloc((strlen(word1) + strlen(word2) + 1) * sizeof(char));
		
	for(uint8_t i=0;i<SIZE;i++)
	{
		g_arry[i]=res[i];
	}
    memset(res, '\0', (strlen(word1) + strlen(word2) + 1));
		
	for(uint8_t i=0;i<SIZE;i++)
	{
		g_arry[i]=res[i];
	}
    int index = 0;
    int iter = 0;
    while (index < strlen(word1) || index < strlen(word2)) 
    {
        if (index < strlen(word1)) 
        {
            res[iter++] = word1[index];
        }
        if (index < strlen(word2)) 
        {
            res[iter++] = word2[index];
        }
        index++;
    }
    return res;
}

#define SIZE 50
char g_arry[50];
void test2(void)
{
	char word1[SIZE]="abc";
	char word2[SIZE]="pqr";
	//char word3[SIZE];
	char *word3;
	word3 =  (char*)malloc(SIZE);
	word3=mergeAlternately(word1,word2);
	
	for(uint8_t i=0;i<SIZE;i++)
	{
		g_arry[i]=word3[i];
	}
}
/*
对于字符串 s 和 t，只有在 s = t + ... + t（t 自身连接 1 次或多次）时，我们才认定 “t 能除尽 s”。
给定两个字符串 str1 和 str2 。返回 最长字符串 x，要求满足 x 能除尽 str1 且 x 能除尽 str2 。

示例 1:
输入：str1 = "ABCABC", str2 = "ABC"
输出："ABC"
示例 2:
输入：str1 = "ABABAB", str2 = "ABAB"
输出："AB"
示例 3：
输入：str1 = "LEET", str2 = "CODE"
输出：""
*******/

//char *gcdOfStrings(char *str1, char *str2)
//{ 
//    if (!(strstr(str1, str2) == str1 || strstr(str2, str1) == str2))
//        return "";
//    int p_a = 0, p_b = 0, cmp = 0;
//    do
//    {
//        cmp = strcmp(str1 + p_a, str2 + p_b);
//        cmp > 0 ? (p_a += strlen(str2 + p_b)) : (p_b += strlen(str1 + p_a)); 
//        if (!(strstr(str1 + p_a, str2 + p_b) == (str1 + p_a) || strstr(str2 + p_b, str1 + p_a) == (str2 + p_b)))
//            return "";
//    } while (cmp);
//    return str1 + p_a;
//}
// 
char * gcdOfStrings(char * str1, char * str2)
{
    char *lger = strlen(str1) > strlen(str2) ? str1 : str2;
    char *sher = strlen(str1) > strlen(str2) ? str2 : str1;

    if (strcmp(lger, sher) == 0) {
        return sher;
    }

    if (strncmp(lger, sher, strlen(sher)) != 0) {
        return "";
    } 

    return gcdOfStrings(lger + strlen(sher), sher);
}

char a[SIZE]="abcabc";
char b[SIZE]="abc";
char *c;

void test3(void)
{
	
	//c =  (char*)malloc(SIZE);
	c= gcdOfStrings(a,b);
	printf("c=%s\r\n",c);
}

char aei[10]="aeiouAEIOU";

uint8_t returnAeiou(char s)
{
	for(int i=0;i<10;i++)
	{
		if(s==aei[i])
			return 1;
	}
	return 0;
}

char* reverseVowels(char* s) 
{
	int n=strlen(s);
	 int i=0,j=n-1;
	while(i<j)
	{
		while( (i<n) && (returnAeiou(s[i])==0) )
		{
			i++;
		}
		
		while( (j>0) && (returnAeiou(s[j])==0) )
		{
			j--;
		}

		if(i<j)
		{
			char tmp=s[i];
			 
			s[i]=s[j];
			s[j]=tmp;
			i++;
			j--;
		}
	}
	return s;
}
char  g_bb[9];
void test4(void)
{
	char ss[]="leetcode";
	char* bb;
	
	bb=reverseVowels(ss);
	for(int i=0;i<9;i++)
	{
		g_bb[i]=bb[i];
	}
	printf("str= %s,ok \r\n",g_bb);
}

//void swap(int* a,int* b)
//{
//    int tmp;
//    tmp=*a;
//    *a=*b;
//    *b=tmp;
//}

//void moveZeroes(int* nums, int numsSize)
//{
//    for(int i=0;i<numsSize;i++)
//    {
//        if(nums[i]==0)
//        {
//			int j=i+1;
//			while(nums[j]==0)
//			{
//				j++;
//			}
//            swap(&(nums[i]),&(nums[j]));
//        }
//    }
//}

void swap(int *a, int *b) 
{
    int t = *a;
    *a = *b, *b = t;
}


void moveZeroes(int *nums, int numsSize)
{
    int left = 0, right = 0;
    while (right < numsSize) 
	{
        if (nums[right])
		{
            swap(nums + left, nums + right);
            left++;
        }
        right++;
    }
}
//

//bool isSubsequence(char* s, char* t)
//{
//    //int n = strlen(s), m = strlen(t);
//	//look[0][0]=1;
//	
//    //int f[m + 1][26];
//    memset(f, 0, sizeof(f));
//    for (int i = 0; i < 26; i++) 
//	{
//        f[m][i] = m;
//    }

//    for (int i = m - 1; i >= 0; i--) 
//	{
//        for (int j = 0; j < 26; j++) 
//		{
//            if (t[i] == j + 'a')
//                f[i][j] = i;
//            else
//                f[i][j] = f[i + 1][j];
//        }
//    }
//    int add = 0;
//    for (int i = 0; i < n; i++) 
//	{
//        if (f[add][s[i] - 'a'] == m) 
//		{
//            return false;
//        }
//        add = f[add][s[i] - 'a'] + 1;
//    }
//    return true;
//}
// 

uint8_t n=2,m=5;

int f[6][26];
int i,j;
void test()
{ 
	char s[2]="ab";
	char t[5]="ahbgd";
	
	bool judeg;
	
    //int n = strlen(s), m = strlen(t);
	//look[0][0]=1;
	
    //int f[m + 1][26];
    memset(f, 0, sizeof(f));
    for ( i = 0; i < 26; i++) 
	{
        f[m][i] = m;
    }

    for ( i = m - 1; i >= 0; i--) 
	{
        for ( j = 0; j < 26; j++) 
		{
            if (t[i] == j + 'a')
                f[i][j] = i;
            else
                f[i][j] = f[i + 1][j];
        }
    }
    int add = 0;
    for ( i = 0; i < n; i++) 
	{
        if (f[add][s[i] - 'a'] == m) 
		{
            judeg=0;//     return false;
        }
        add = f[add][s[i] - 'a'] + 1;
    }
    judeg=1;//    return true;
	

	printf("judeg= %d\r\n",judeg);
	
}
/**zhangzhihui*
A578588888
**/

